﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using BLL;
using Model;

public partial class Assemble_AssembleStepAdd : System.Web.UI.Page
{    
    private AssembleStep ams;
    private DataRow dr;
    private DataTable amsdt
    {
        get
        {
            return (DataTable)ViewState["amsdt"];
        }
        set 
        {
            ViewState["amsdt"] = value;
        }
    }

    public Assemble_AssembleStepAdd()
    {
        ams = new AssembleStep();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            amsdt = new DataTable();
            amsdt.Columns.Add(new DataColumn("PostName", Type.GetType("System.String")));
            amsdt.Columns.Add(new DataColumn("UserID", Type.GetType("System.String")));
        }
        //Bindams();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup2");
        if (Page.IsValid)
        {
            dr = amsdt.NewRow();
            dr["PostName"] = this.TextBox2.Text.Trim();
            dr["UserID"] = this.TextBox3.Text.Trim();
            if (amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "' and UserID = '" + this.TextBox3.Text.Trim() + "'").Length == 0)
            {
                if (amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "'").Length > 0)
                    amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "'")[0].Delete();
                amsdt.Rows.InsertAt(dr, 0);
                Bindams();
                this.TextBox2.Text = "";
                this.TextBox3.Text = "";
                this.TextBox2.Focus();
            }
            else
            {
                string str = "已经在列表中";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        this.TextBox2.Focus();
    }

    protected void Bindams()
    {
        this.GridView1.DataSource = amsdt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        amsdt.Select("PostName = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "' and UserID = '" + GridView1.Rows[e.RowIndex].Cells[1].Text + "'")[0].Delete();
        Bindams();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup1");
        if (Page.IsValid)
        {
            string str = "";
            if (amsdt.Rows.Count < 1)
            {
                str = "请先添加关键工序信息!";
            }
            else
            {
                string productSN = this.TextBox1.Text.Trim();
                if (CommHelper.IsValidSN_forDataAcquisition(productSN))
                {
                    productSN = CommHelper.GetProductSN(productSN);
                    DateTime dt = DateTime.Now;
                    AssembleStepInfo asi = new AssembleStepInfo(productSN, dt, User.Identity.Name);
                    if (ams.AssembleStepItem_Insert(asi, amsdt))
                    {
                        str = " 提交成功!";
                        this.TextBox1.Text = "";
                        this.TextBox1.Focus();
                    }
                    else
                    {
                        str = "提交失败，请联系相关人员!";
                    }
                }
                else
                {
                    str = "序列号不符合腔体序列号编码规则，或不是关联过的成品序列号，请检查!";
                }
            }
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }
}
